<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html xmlns:th="" lang="zh-CN">
<%@include file="/WEB-INF/pages/base/include.jsp"%>
<body>
<div id="wrapper">
    <%@include file="/WEB-INF/pages/base/menu.jsp"%>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">供应商管理</h3>
            </div>
        </div>
        <div class="row">
            <form class="form-horizontal" data-parsley-validate>
                <div class="col-lg-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">供应商基本信息</div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-12 col-md-12">
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">供应商名称：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="name" class="form-control" placeholder="请输入供应商名称"
                                                   required>
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">供应商公司：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="company" class="form-control"
                                                   placeholder="请输入供应商公司">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">供应商地址：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="address" class="form-control"
                                                   placeholder="请输入供应商地址">
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">承接页限制商品数量(仅主账号修改有效)：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="undertakeCount" class="form-control"
                                                   placeholder="请输入承接页限制商品数量">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                         <label class="col-lg-2 clo-md-2 control-label">供应商logo：</label>
                                         <div class="col-lg-8">
                                             <p><img id="logo" src="" width="50px"></p>
                                             <button class="btn btn-default" id="photoUpload_1">上传图片</button>
                                         </div>
                                     </div>
                                     <div class="form-group no-filereader-hidden">
                                        <label class="col-sm-2 control-label"></label>
                                        <div class="col-sm-10">
                                            <div class="tag-wrapper" id="attachmentTag_1"></div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">供应商状态：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="status" class="form-control"
                                                   placeholder="请输入供应商状态">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
                <button type="button" class="btn btn-primary" onclick="productSeller_modify()"
                        style="margin-left:15px;margin-bottom:30px;">发布
                </button>
                <button class="btn btn-default" id="cancel" style="margin-left:15px;margin-bottom:30px;">
                    取消
                </button>
            </form>
        </div>
    </div>
</div>
<script>
	var images = [],descs=[];
	var currentI = 0;
	$(function(){
		var productSellerId = "${productSellerId}";
		var baseURL = "${webPath}/productSeller/detail";
	  	var json_package = {
	       "head": {},
	       "body": {
	       	"productSellerId": productSellerId
	       }
	   	}
		$.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	      if (typeof result == 'string') result = JSON.parse(result);
	      if (result.body.result == '1') {
	          alert(result.body.description)
	      } else {
	      	var productSeller = result.body.productSeller;
	      	$("#name").val(productSeller.name);
	      	$("#company").val(productSeller.company);
	      	$("#address").val(productSeller.address);
	      	$("#undertakeCount").val(productSeller.undertakeCount);
	      	$("#logo").attr('src',productSeller.logo); 
	      	$("#status").val(productSeller.status);
	      }
	  	})
	});
	
	(function(){
		var ajaxUpload = new AjaxUpload($('#photoUpload_1'), {
		    action: '${webPath}/file/uploadImage',
		    data: {
		        "json_package": JSON.stringify({
		            "head": {},
		            "body": {"file": "file"}
		        })
		    },
		    name: 'file',
		    onSubmit: function (file, ext) {
		        window.uploading = true;
		        $('#photoUpload_1').text('上传中...');
		        $('#photoUpload_1').attr('disabled', true);
		        this.disable();
		
		    },
		    onChange: function (file, extension) {
		        this.disable();
		    },
		    onComplete: function (file, response) {
		    	var data = JSON.parse(response);
		    	fileUrl = data.body.fileUrl;
		    	$("#logo").attr('src',fileUrl); 
		        window.uploading = false;
		        $('#photoUpload_1').text('上传图片');
		        this.enable();
		    }
		});
	})();
	
	function productSeller_modify() {
		
		var type = "${type}";
		if(type == "3"){
			alert("当前账号无权限使用此功能");
			return;
		}
		
    	var baseURL = "${webPath}/productSeller/modify";
    	var productSellerId = "${productSellerId}";
        var name = $('#name').val();
        if(name.length<=0){
        	alert("供应商名称不能为空");
        	return;
        }
        var company = $('#company').val();
        var address = $('#address').val();
        if(address.length<=0){
            alert("供应商地址不能为空");
            return;
        }
        var undertakeCount = $('#undertakeCount').val();
        var logo = $("#logo")[0].src; 
        var status = $('#status').val();
        
        var json_package = {
       		"head": {},
            "body": {
            	"productSellerId": productSellerId,
                "name": name,
                "company": company,
                "address": address,
                "undertakeCount": undertakeCount,
                "logo":logo,
                "status": status
            }
        };
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('修改成功！')
                window.location.reload(true);
            } else {
                alert('修改失败！')
            }
        })
    }
	$(function () {
        $('#cancel').click(function () {
            window.location.href = "${webPath}toSeller";
        })
    });
    $(document).ready(function (e) {
        $('#pro_info').keydown(function () {
            var len = $(this).val().length;
            if (len >= 100) {
                var num = $(this).val().substring(0, 99);
                $(this).val(num);
                alert("超过字数限制")
            }
        })
    });
    $(document).ready(function (e) {
        $('#pro_desc').keydown(function () {
            var len = $(this).val().length;
            if (len >= 140) {
                var num = $(this).val().substring(0, 139);
                $(this).val(num);
                alert("超过字数限制")
            }
        })
    });
</script>
</body>
</html>